import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '../store/index'
// import Goods from '../views/Goods.vue'

Vue.use(VueRouter)



const router = new VueRouter({
  routes:[
      {
        path: '/',
        component:()=>import ('../views/Main.vue'),
        children:[
          {
            path: '/',
            component: Home
          },
          {
            path: 'cart',
            component: () => import('../views/Cart.vue')
          },
          {
            path: 'classfiy',
            component: () => import('../views/Classfiy.vue'),
          },
          {
            path: 'center',
            component: () => import('../views/Center.vue'),
          },
        ]
      },
      {
        path: '/detail/:id',
        component: () => import('../views/Detail.vue')
      },
      {
        path: '/goods/:id',
        component: () => import('../views/Goods.vue')
      }, 
      {
        path: '/addressadd',
        component: () => import('../views/Addressadd.vue')
      },
      {
        path: '/login',
        component: () => import('../views/Login.vue')
      },
      {
        path: '/reg',
        component: () => import('../views/Reg.vue')
      },
      {
        path: '/payCart',
        component: () => import('../views/payCart.vue')
      },
      
    ]
})

// 路由守卫
// let isLogin=false
router.beforeEach((to,from,next)=>{
  if(to.path.indexOf('/center')!=-1){
    // console.log(store.state.user.userInfo)
    if(store.state.user.userInfo){
      next()
    }else{
      next({
        path:'/login',
        query:{
            target:to.path
        }
    })
    }
  }else{
    next()
  }
})

export default router
